/*
 * Copyright (C) 2000-2008 Jens Nixdorf and various contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.mopore.jat.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.geom.RoundRectangle2D;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mopore.jat.core.CoreInfo;
import org.mopore.jat.core.CorePlatformPlugin;
import org.mopore.jat.plugin.IPluginFileAccess;
import org.mopore.jat.plugin.PluginFileAccessJaxB;

import com.sun.awt.AWTUtilities;

public class ProgressDialog extends JFrame {

	private JProgressBar progressBar;
	private String text;
	private Label textLabel;

	private final Log log = LogFactory.getLog(getClass().getName());
	private Color color;
	private Color barColor;
	
	public ProgressDialog( String text ) {
		
		this.text = text;
		setUndecorated(true);
		setSize(300, 50);
		barColor = new Color(255, 150, 0);
		
		try {
			IPluginFileAccess fileAccess = new PluginFileAccessJaxB( CorePlatformPlugin.class );
			CoreInfo coreInfo = fileAccess.retrieve( CoreInfo.class );
			Theme theme = coreInfo.getTheme();
			Image image = theme.getIcon().getImage();
			setIconImage( image );
		} catch (IOException e) {
			log.error( "could not retrieve theme", e );
		}
		
		Toolkit t = this.getToolkit();
		this.setLocation(
				(int) ((t.getScreenSize().getWidth() - this.getWidth()) / 2),
				(int) ((t.getScreenSize().getHeight() - this.getHeight()) / 2));
		setTitle("mopore-jat processing");

		Shape shape = new RoundRectangle2D.Float(0, 0, 300, 50, 10, 20);
		AWTUtilities.setWindowShape( this, shape );
		AWTUtilities.setWindowOpacity( this, 0.9f );

		JPanel panel = createProgressDialogContent();
		setContentPane(panel);
		setVisible(true);
		requestFocus();
	}

	private JPanel createProgressDialogContent() {

		JPanel panel = new JPanel();
		color = new Color( 240, 240, 240 );
		panel.setBackground( color );
		GridLayout gridLayout = new GridLayout(2, 1);
		panel.setLayout(gridLayout);
		Insets insets = new Insets(5, 10, 5, 10);
		panel.setBorder(new EmptyBorder(insets));

		textLabel = new Label( text );
		textLabel.setFont( new Font("Arial", Font.PLAIN, 12));
		panel.add(textLabel);
		this.progressBar = new JProgressBar();
		progressBar.setStringPainted( true );
		progressBar.setMaximum(100);
		progressBar.setForeground( barColor );
		panel.add(progressBar);

		return panel;
	}

	public void setProgress(int perCent) {
		progressBar.setValue(perCent);
	}

	public void setText( String text ) {
		textLabel.setText( text );
	}
	
	public static void main(String[] args) throws Exception {

		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		
		ProgressDialog progressDialog = new ProgressDialog( "Adding plugin..." );
		progressDialog.setProgress( 81 );
		Thread.sleep(5000);
		progressDialog.dispose();

	}




}
